public class SqlScriptsTestExecutionListener extends AbstractTestExecutionListener
TestExecutionListener
that provides support for executing SQL scripts
configured via the @Sql
annotation.
Scripts will be executed before
or after execution of the corresponding
test method, depending on the configured
value of the executionPhase
flag.
Scripts will be executed without a transaction, within an existing
Spring-managed transaction, or within an isolated transaction, depending
on the configured value of SqlConfig.transactionMode()
and the
presence of a transaction manager.
For details on default script detection and how explicit script locations
are interpreted, see Sql.scripts()
.
A PlatformTransactionManager
and a DataSource
,
just a PlatformTransactionManager
, or just a DataSource
must be defined as beans in the Spring ApplicationContext
for the
corresponding test. Consult the javadocs for SqlConfig.transactionMode()
,
SqlConfig.transactionManager()
, SqlConfig.dataSource()
,
TestContextTransactionUtils.retrieveDataSource(org.springframework.test.context.TestContext, java.lang.String)
, and
TestContextTransactionUtils.retrieveTransactionManager(org.springframework.test.context.TestContext, java.lang.String)
for details
on permissible configuration constellations and on the algorithms used to
locate these beans.
Sql
,
SqlConfig
,
SqlGroup
,
TestContextTransactionUtils
,
TransactionalTestExecutionListener
,
ResourceDatabasePopulator
,
ScriptUtils
Modifier and Type | Field and Description |
---|---|
private static Log |
logger |
HIGHEST_PRECEDENCE, LOWEST_PRECEDENCE
Constructor and Description |
---|
SqlScriptsTestExecutionListener() |
Modifier and Type | Method and Description |
---|---|
void |
afterTestMethod(TestContext testContext)
Execute SQL scripts configured via
@Sql for the supplied
TestContext after the current test method. |
void |
beforeTestMethod(TestContext testContext)
Execute SQL scripts configured via
@Sql for the supplied
TestContext before the current test method. |
private java.lang.String |
detectDefaultScript(TestContext testContext,
boolean classLevel)
Detect a default SQL script by implementing the algorithm defined in
Sql.scripts() . |
private void |
executeSqlScripts(Sql sql,
Sql.ExecutionPhase executionPhase,
TestContext testContext,
boolean classLevel)
Execute the SQL scripts configured via the supplied
@Sql
annotation for the given Sql.ExecutionPhase and TestContext . |
private void |
executeSqlScripts(TestContext testContext,
Sql.ExecutionPhase executionPhase)
|
private javax.sql.DataSource |
getDataSourceFromTransactionManager(PlatformTransactionManager transactionManager) |
int |
getOrder()
Returns
5000 . |
private java.lang.String[] |
getScripts(Sql sql,
TestContext testContext,
boolean classLevel) |
afterTestClass, beforeTestClass, prepareTestInstance
public final int getOrder()
5000
.getOrder
in interface Ordered
getOrder
in class AbstractTestExecutionListener
public void beforeTestMethod(TestContext testContext) throws java.lang.Exception
@Sql
for the supplied
TestContext
before the current test method.beforeTestMethod
in interface TestExecutionListener
beforeTestMethod
in class AbstractTestExecutionListener
testContext
- the test context in which the test method will be
executed; never null
java.lang.Exception
- allows any exception to propagatepublic void afterTestMethod(TestContext testContext) throws java.lang.Exception
@Sql
for the supplied
TestContext
after the current test method.afterTestMethod
in interface TestExecutionListener
afterTestMethod
in class AbstractTestExecutionListener
testContext
- the test context in which the test method was
executed; never null
java.lang.Exception
- allows any exception to propagateprivate void executeSqlScripts(TestContext testContext, Sql.ExecutionPhase executionPhase) throws java.lang.Exception
java.lang.Exception
private void executeSqlScripts(Sql sql, Sql.ExecutionPhase executionPhase, TestContext testContext, boolean classLevel) throws java.lang.Exception
@Sql
annotation for the given Sql.ExecutionPhase
and TestContext
.
Special care must be taken in order to properly support the configured
SqlConfig.transactionMode()
.
sql
- the @Sql
annotation to parseexecutionPhase
- the current execution phasetestContext
- the current TestContext
classLevel
- true
if @Sql
was declared at the
class leveljava.lang.Exception
private javax.sql.DataSource getDataSourceFromTransactionManager(PlatformTransactionManager transactionManager)
private java.lang.String[] getScripts(Sql sql, TestContext testContext, boolean classLevel)
private java.lang.String detectDefaultScript(TestContext testContext, boolean classLevel)
Sql.scripts()
.